package demo;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-04-09
 * Time: 21:37
 */
public class MySingleList {

    static class ListNode{
        public int val;
        public ListNode next;
        ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
            node.next = head;
            head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(head == null){
            head = node;
            return;
        }
        while (cur != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNode node = new ListNode(data);
        ListNode cur = head;
        int len = size();
        if(index < 0 || index > len){
            throw new IndexOutOfBounds("任意位置插入，位置不合法");
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == len) {
            addLast(data);
            return;
        }
        for (int i = 0; i < len; i++) {
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null){
            if(cur.val == key){
                return true;
            }else{
                cur = cur.next;
            }
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode node = new ListNode(key);
        ListNode cur = head;
        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            return;
        }
        boolean prev = contains(key);
        if(!prev) {
            System.out.println("没有这个数据！");
            return;
        }
        while (cur.next != null){
            if(cur.next.val == key){
                cur.next = node.next;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        ListNode cur = head.next;
        ListNode prev = head;
        while (cur != null) {
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            } else{
                prev = cur;
                cur = cur.next;
            }
        }
            if(head.val ==  key) {
            head = head.next;
            }
    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;

        }
        return count;
    }

    //打印
    public void display(){
        ListNode cur = head;
        while (cur != null){
            System.out.println(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //清空
    public void clear(){
        this.head = null;
    }

}
